package top.lc951.myandroid;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bartoszlipinski.flippablestackview.FlippableStackView;
import com.bartoszlipinski.flippablestackview.StackPageTransformer;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import top.lc951.bannerview2.BannerUtils;
import top.lc951.bannerview2.BannerViewPager;
import top.lc951.bannerview2.constants.PageStyle;
import top.lc951.bannerview2.view.CatchViewPager;
import top.lc951.myandroid.model.BaseResponse;
import top.lc951.myandroid.model.CardOverlayModel;
import top.lc951.myandroid.views.card.CardOverlayAdapter;
import top.lc951.myandroid.views.card.CardOverlayTransformerCenter;
import top.lc951.myandroid.views.card.OverlayTransformerLan;
import top.lc951.myandroid.views.card.SimpleOverlayAdapter;
import top.lc951.myandroid.views.card2.Card2OverlayAdapter;
import top.lc951.myandroid.views.card2.FlipperCardView;
import top.lc951.myandroid.views.card3.CardOverlayViewHolder;

/**
 * 广告页 卡片叠加展示
 * Card overlay display
 */
public class CardOverlayActivity extends AppCompatActivity {
    public static final String TAG = CardOverlayActivity.class.getSimpleName();
    private ViewPager viewPager;
    private OkHttpClient client;

    public static void actionActivity(Context context) {
        Intent intent = new Intent(context, CardOverlayActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_overlay);
        bindViewIds();
        client = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();
                        Response response = chain.proceed(request);
                        return response;
                    }
                })
                .connectTimeout(4000, TimeUnit.MILLISECONDS)
                .readTimeout(4000, TimeUnit.MILLISECONDS)
                .writeTimeout(4000, TimeUnit.MILLISECONDS)
                .build();
        apiData();
    }


    private void bindViewIds() {
        viewPager = findViewById(R.id.view_pager);

    }

    private void apiData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String postManUrl = "https://339b224f-a379-4bac-923f-675bf8aa8476.mock.pstmn.io/hotmp4";
                String apipostUrl = "https://console-mock.apipost.cn/app/mock/project/e506875e-055f-452a-c781-e00e05f2f9f9" +
                        "/hotmp4";
                /* 如果需要参数 , 在url后边拼接 :  ?a=aaa&b=bbb..... */
                Request request = new Request.Builder()
                        .url(apipostUrl)
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    public void onResponse(Call call, final Response response) throws IOException {
                        final String result = response.body().string();
                        final boolean ok = response.isSuccessful();
                        Log.i(TAG, "result=" + result);
                        BaseResponse<CardOverlayModel> baseModel = new Gson().fromJson(result
                                , new TypeToken<BaseResponse<CardOverlayModel>>() {
                                }.getType());
                        Log.i(TAG, "data=" + baseModel.getLivingRoomList());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showCardOverlayData(baseModel);
                            }
                        });
                    }

                    public void onFailure(Call call, IOException e) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                Toast.makeText(CardOverlayActivity.this, "error", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        }).start();


    }

    private void showCardOverlayData(BaseResponse<CardOverlayModel> baseModel) {
        if (null == baseModel.getLivingRoomList() || 0 == baseModel.getLivingRoomList().size())
            return;
        List<CardOverlayModel> list = baseModel.getLivingRoomList();
        viewPager.setOffscreenPageLimit(3);
//        viewPager.setOverlapStyle(true);
        CardOverlayAdapter pagerAdapter = new CardOverlayAdapter(this);
        pagerAdapter.setImgUrlsAndBindViewPager(viewPager, list, 3);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        FlippableStackView stack = (FlippableStackView) findViewById(R.id.stack);
        stack.initStack(list.size(), StackPageTransformer.Orientation.HORIZONTAL
                , 0.8f, 0.7f
                , 0.4f, StackPageTransformer.Gravity.TOP);
        stack.setAdapter(pagerAdapter); //assuming mStackAdapter contains your initialized adapter


        BannerViewPager<CardOverlayModel, CardOverlayViewHolder> bannerViewPager = findViewById(R.id.banner_view);
        bannerViewPager.setOnPageClickListener(new BannerViewPager.OnPageClickListener() {
            @Override
            public void onPageClick(int position) {
                Toast.makeText(CardOverlayActivity.this, "position:" + position, Toast.LENGTH_SHORT)
                        .show();
            }
        })
                .setAutoPlay(false)
                .setPageStyle(PageStyle.MULTI_PAGE_OVERLAP_SCALE)
                .setRevealWidth(BannerUtils.dp2px(30))
                .setHolderCreator(() -> new CardOverlayViewHolder(this))
                .setOffScreenPageLimit(3)
                .setCanLoop(false)
                .create(list);
    }
}